﻿#region Header

// MS codename Velocity Distributed Cache Extensions.
// (c) 2009

#endregion


#region Usings

using System;
using System.Configuration;

#endregion


namespace Caching.Extensions.Core.Config
{
    /// <summary>
    /// Named cache instance configuration.
    /// </summary>
    /// <remarks>
    /// sample config section
    /// <code>
    ///&lt;smartCache&gt;
    ///  &lt;namedCaches&gt;
    ///    &lt;cache name="SmartCacheName1" backendCacheName="SecondLevelCacheName1" ttl="10 (seconds)" autoUpdate="true"/&gt;
    ///    &lt;cache name="SmartCacheName2" backendCacheName="SecondLevelCacheName2" ttl="10 (seconds)" autoUpdate="true"/&gt;
    ///  &lt;/namedCaches&gt;
    ///&lt;/smartCache&gt;
    /// </code>
    /// </remarks>
    public class SmartCacheInstanceConfig : ConfigurationElement
    {
        #region Properties

        /// <summary>
        /// SmartCache tag name
        /// </summary>
        [ConfigurationProperty("name", DefaultValue = "", IsKey = true, IsRequired = true)]
        public string Name
        {
            get { return (string) this["name"]; }
        }


        /// <summary>
        /// Underlying cache name 
        /// </summary>
        [ConfigurationProperty("backendCacheName", DefaultValue = "", IsRequired = true)]
        public string SecondLevelCacheName
        {
            get { return (string) base["backendCacheName"]; }
        }

        /// <summary>
        /// Local cache TTL
        /// </summary>
        [ConfigurationProperty("ttl", DefaultValue = "00:00:10", IsRequired = true)]
        [TimeSpanValidator(MinValueString = "00:00:10", MaxValueString = "10:00:00", ExcludeRange = false)]
        public TimeSpan Ttl
        {
            get
            {
                object ttl = this["ttl"];
                return ttl != null ? (TimeSpan) ttl : TimeSpan.FromMinutes(1);
            }
        }

        /// <summary>
        /// Auto update items from SecondLevelCache on get
        /// </summary>
        [ConfigurationProperty("autoUpdate", DefaultValue = false)]
        public bool AutoUpdate
        {
            get { return (bool) this["autoUpdate"]; }
        }

        #endregion
    }
}